<template>
<div class="container">
    <div class="page-head">
        <div class="left">
            <el-form :inline="true" :model="query" @submit.native.prevent>
                <el-form-item>
                    <el-button type="danger" @click="showCreateView"><i class="el-icon-plus" style="margin-right: 5px"></i>添加</el-button>
                </el-form-item>
                <el-form-item>
                    <picker-view v-model="query.company_id" type="company" placeholder="选择公司"></picker-view>
                </el-form-item>
                <el-form-item>
                    <picker-view v-model="query.timing" type="timing" placeholder="选择审核类型"></picker-view>
                </el-form-item>
                <el-button type="primary" native-type="submit" @click="doSearch">搜索</el-button>
            </el-form>
        </div>
    </div>

    <el-table ref="tableView" height="tableHeight"  class="page-body" v-loading="loading" :data="tableData" border>
        <el-table-column label="编号" prop="id" width="100" align="center"></el-table-column>
        <el-table-column label="公司名称" prop="company.title"></el-table-column>
        <el-table-column label="审核类型" prop="tag.title" width="120"></el-table-column>
        <el-table-column label="上/下线" prop="is_valid" width="120">
            <template slot-scope="scope">
                <el-tag type="primary" v-if="scope.row.is_valid == 1">使用中</el-tag>
                <el-tag type="danger" v-else>已下线</el-tag>
            </template>
        </el-table-column>
        <el-table-column label="排序" prop="sort" width="120"></el-table-column>
        <el-table-column label="创建时间" prop="create_time" width="180"></el-table-column>
        <el-table-column label="操作" align="center">
            <template slot-scope="scope">
                <el-button type="primary" size="mini" @click="showEditView(scope.row)">编辑</el-button>
                <el-button type="danger" size="mini" @click="toFlow(scope.row)">配置审核</el-button>
                <el-button type="warning" size="mini" @click="deleteRow(scope.row)">删除</el-button>
            </template>
        </el-table-column>
    </el-table>

    <div class="page-foot">
        <div class="left">

        </div>
        <div class="right">
            <common-pagination :total="pg.total" :page.sync="pg.page" :limit.sync="pg.limit" @getList="getTableData"></common-pagination>
        </div>
    </div>

    <save-view ref="editView" @getTableData="getTableData"></save-view>
</div>
</template>

<script>
import pickerView from "@/components/picker/select.vue";
import saveView from './save';
import pagination from "@/components/mixins/pagination.vue";

import {
    getPageDataApi,
    deleteApi
} from "@/api/audit/config"

export default {
    components: {
        pickerView,
        saveView
    },
    mixins: [pagination],
    data() {
        return {
            loading: false,
            query: {
                company_id: "",
                timing: ""
            },
            tableData: [],
        };
    },
    computed: {
        companyList: function () {
            return [];
        }
    },
    created() {
        this.getTableData();
    },
    methods: {
        doSearch() {
            this.resetPg();
            this.getTableData();
        },
        getTableData() {
            let params = Object.assign({}, this.query);
            params["pagination"] = this.pg;
            getPageDataApi(params).then((res) => {
                if (res.code == 200) {
                    this.tableData = res.data.list;
                    this.setPg(res);
                } else {
                    this.$message.warning(res.msg);
                }
            });
        },
        showCreateView() {
            if (this.$refs["editView"] != undefined) {
                this.$refs["editView"].showAddView();
            } else {
                this.$nextTick(function () {
                    this.$refs["editView"].showAddView();
                });
            }
        },
        showEditView(row) {
            if (this.$refs["editView"] != undefined) {
                this.$refs["editView"].showSaveView(row);
            } else {
                this.$nextTick(function () {
                    this.$refs["editView"].showSaveView(row);
                });
            }
        },
        deleteRow(row) {
            this.$confirm("确认删除该条信息?", "警告", {
                    type: "warning",
                })
                .then((res) => {
                    deleteApi(row)
                        .then((res) => {
                            if (res.code == 200) {
                                this.getTableData();
                                this.$message.success(res.msg);
                            } else {
                                this.$message.error(res.msg);
                            }
                        })
                        .catch((res) => {
                            this.$message.info("系统异常，请联系管理员");
                        });
                })
                .catch((res) => {
                    this.$message.info("您取消了删除操作");
                });
        },
        toFlow(row) {
            this.$router.push({
                path: "/audit/config/flow",
                query: {
                    id: row.id
                }
            })
        }
    }
}
</script>
